'''
爬取图片并下载图片

url:https://pic.netbian.com/4kmeinv/

'''

import requests
import re
from bs4 import BeautifulSoup
import os

#爬取网页requests

# url = "https://pic.netbian.com/4kmeinv/"
# url2 = "https://pic.netbian.com/uploads/allimg/221216/095854-1671155934fe32.jpg"

#抓取指定url网页资源函数
def craw_html(url):
    #获取网页资源
    response = requests.get(url)
    response.encoding = 'gbk'
    print(response.status_code)
    html = response.text
    # print(html)
    return html

def parse_and_download(html):
    #解析图片的地址
    soup = BeautifulSoup(html, "html.parser")
    imgs = soup.find_all("img")
    for img in imgs:
        src = img["src"]
        if "/uploads/" not in src:
            continue
        src = f"https://pic.netbian.com{src}"
        print(src)

       # 首先得到图片的本地文件地址
        filename = os.path.basename(src)
        with open(f"美女图片/{filename}", "wb+") as fp:
            resp_img = requests.get(src)
            fp.write(resp_img.content)
            print("爬取图片结束")
            # fp.close()




urls = [
    f"https://pic.netbian.com/4kmeinv/index_{i}.html"
    for i in range(2,11)
]

print(urls)

for url in urls:
    print("#####正在爬取:", url)
    html = craw_html(url)
    parse_and_download(html)